package djboom.materials.display.page.work3 {
	import djboom.materials.Work;
	import djboom.materials.core.BitmapDataCore;
	import djboom.materials.core.DataCore;
	import djboom.materials.display.elements.RoundedButton;
	import djboom.materials.display.elements.SampleInfo;
	import djboom.materials.display.page.Page;
	import djboom.materials.events.PageEvent;

	import org.casalib.display.CasaSprite;

	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * .
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 28.04.2010
	 * 
	 */

	public class Page3 extends Page {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _simpleButton:RoundedButton;
		private var _samplesList:XMLList;
		private var _samples:CasaSprite;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function Page3() {
			super();
		}	 

		// ----------------------------------------------------------------------------
		// Методы класса.
		// ----------------------------------------------------------------------------

		private function changePage(event:MouseEvent):void {
			var pageEvent:PageEvent = new PageEvent(PageEvent.CHANGE);
			pageEvent.pageName = Page.WORK_3_PAGE_4;            
			dispatchEvent(pageEvent);
		}

		// ----------------------------------------------------------------------------
		// Публичные методы.
		// ----------------------------------------------------------------------------

		override public function init():void {
			// Создаем заголовок страницы.
			createTitleWork(WORK3_NAME, 3);
			
			_samplesList = DataCore._materialsWork3XML.child("material").(@select == "true");

			_samples = new CasaSprite();
			_samples.x = 40;
			_samples.y = 100;
			addChild(_samples);
             
			for each (var sampleXML:XML in _samplesList) {
				var sampleName:String = sampleXML.attribute("name").toString();				var sampleDescription:String = sampleXML.attribute("description").toString();
				var sampleBitmapData:String = sampleXML.attribute("bitmapdata").toString();
            
				var sampleInfo:SampleInfo = new SampleInfo(sampleName, sampleDescription, BitmapDataCore.getBitmapData(sampleBitmapData));
				_samples.addChild(sampleInfo);
				
				try {
					var lastSampleInfo:SampleInfo = _samples.getChildAt(_samples.numChildren - 2) as SampleInfo;
					sampleInfo.y = lastSampleInfo.y + lastSampleInfo.height + 30;
				} catch(error:Error) {
				}
			}
			
			_simpleButton = new RoundedButton("Продолжить");
			_simpleButton.x = Work.WIDTH * 0.5 - _simpleButton.width * 0.5;
			_simpleButton.y = Work.HEIGHT - 118;
			_simpleButton.addEventListener(MouseEvent.CLICK, changePage);
			addChild(_simpleButton);
		}

		override protected function clearPage(event:Event):void {
			super.clearPage(event);
			
			while (_samples.numChildren) {
				var sampleInfo:SampleInfo = _samples.getChildAt(0) as SampleInfo;
                
				sampleInfo.removeChildrenAndDestroy();
				sampleInfo = null;
			}
            
			_samples.destroy();
			_samples = null;
			
			if (_simpleButton != null) {
				_simpleButton.destroy();
				_simpleButton = null;
			}
			
			_samplesList = null;
		}
        
		// ----------------------------------------------------------------------------
        // Публичные свойства.
        // ----------------------------------------------------------------------------
	}
}
